package com.lzw.mall.common.service.impl;
import com.lzw.mall.common.entity.Attachment;
import com.lzw.mall.common.mapper.AttachmentMapper;
import com.lzw.mall.common.service.IAttachmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzw.mall.common.util.Constants;
import com.lzw.mall.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lizhenwei
 * @since 2023-03-18
 */
@Service
public class AttachmentServiceImpl extends ServiceImpl<AttachmentMapper, Attachment> implements IAttachmentService {

    @Autowired
    private AttachmentMapper attachmentMapper;



    @Override
    public Integer doSave(Attachment att) {
        //attachmentMapper.insert(att);
        attachmentMapper.doInsert(att.getPath());
        return att.getId();
    }

    @Override
    public synchronized ResultVO upload(MultipartFile file) {
        
        Attachment att = new Attachment();
        ResultVO vo=new ResultVO();
        String a=(file.getContentType().split("/"))[1];
        String fileName01=System.currentTimeMillis() + "."+a;
        File f=new File("D:\\test01\\attachment\\"+fileName01);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String fileName = file.getOriginalFilename();
        String type = file.getContentType();
        long fileSize = file.getSize();
        att.setFileSize(fileSize);
        att.setFileName(fileName);
        att.setType(type);
        att.setCreatetime(LocalDateTime.now());
        att.setPath(Constants.UPLOAD_PATH+"attachment\\"+fileName01);
        attachmentMapper.insert(att);
        Map<String, Integer> map = new HashMap<>();
        map.put("id", att.getId());
        System.out.println(att.getId());
        vo.setCode(0);
        vo.setData(map);
        return vo;
    }

    @Override
    public void updateSource(String idss, Integer sourceId, String source) {

        String[] ids =idss.split(",");

        attachmentMapper.doUpdateById(ids, sourceId, source);
    }

    @Override
    public List<Attachment> selectList(Integer pid,String source) {
        return attachmentMapper.selectList1(pid,source);
    }

    @Override
    public ResultVO doDeleteById(Integer id) {
        ResultVO vo = new ResultVO();
       attachmentMapper.deleteById(id);
       return vo;
    }

}
